package com.ihd.ihardware.view.tzc.health.view;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.app.MyApplication;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.widget.RulerView;
import com.ihd.ihardware.databinding.TargetTzFragmentBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.health.viewmodel.HandViewModel;

import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.base.BaseFragment;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class RecordWeightFragment extends BaseFragment<TargetTzFragmentBinding, HandViewModel> {

    private UserInfoRes.DataBean ud;

    public RecordWeightFragment() {
    }

    public static RecordWeightFragment newInstance(String param1, String param2) {
        RecordWeightFragment fragment = new RecordWeightFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        ud = new Gson().fromJson(SPUtils.getString(AppConstans.USER_INFO, ""), UserInfoRes.DataBean.class);

        binding.rulerHeight.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                binding.currWeightTV.setText(Float.toString(value));
            }
        });

        if (!TextUtils.isEmpty(ud.getWeight())) {
            binding.currWeightTV.setText(ud.getWeight());
            binding.rulerHeight.setValue(Float.parseFloat(ud.getWeight()), 30.0f, 180.0f, 0.1f);
        } else {
            binding.currWeightTV.setText("60.0");
            binding.rulerHeight.setValue(60.0f, 30.0f, 180.0f, 0.1f);
        }

        binding.finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.handWeighing(MyApplication.memberId.getMemberId(), binding.currWeightTV.getText().toString());
            }
        });

        setRx(AppConstans.HANDWEIGHT, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                DialogUtils.successDialog(getActivity(), MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "添加成功");
                ud.setWeight(binding.currWeightTV.getText().toString());
                SPUtils.putString(AppConstans.USER_INFO, new Gson().toJson(ud, UserInfoRes.DataBean.class));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getActivity().finish();
                    }
                }, 2100);
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(getContext(), error.getMessage());
            }
        });
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.target_tz_fragment;
    }

    @Override
    protected Class getViewModelClass() {
        return HandViewModel.class;
    }
}
